<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form ref="pram" :model="pram" :rules="rules" label-width="100px">
        <el-form-item :label="$t('guanLiYuanZhangHao')" prop="account">
          <el-input v-model="pram.account" :placeholder="$t('guanLiYuanZhangHao')" :disabled="true" />
        </el-form-item>
        <el-form-item label="店铺ID" prop="account">
          <el-input v-model="pram.code" :disabled="true" />
        </el-form-item>
        <el-form-item :label="$t('guanLiYuanXingMing')" prop="realName">
          <el-input v-model="pram.realName" :placeholder="$t('guanLiYuanXingMing')" />
        </el-form-item>
        <el-form-item :label="$t('xinMiMa')" prop="pwd">
          <el-input
            v-model="pram.pwd"
            :placeholder="$t('guanLiYuanMiMa')"
            clearable
            @input="handlerPwdInput"
            @clear="handlerPwdInput"
          />
        </el-form-item>
        <el-form-item v-if="pram.pwd" :label="$t('queRenXinMiMa')" prop="repwd">
          <el-input v-model="pram.repwd" :placeholder="$t('queRenXinMiMa')" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handlerSubmit('pram')">{{ $t('confirm') }}</el-button>
          <el-button @click="close('pram')">{{ $t('cancel') }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import * as systemAdminApi from '@/api/systemadmin.js';
import Cookies from 'js-cookie';
import { Debounce } from '@/utils/validate';
export default {
  name: 'Index',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('qingZaiCiShuRuMiMa')));
      } else if (value !== this.pram.pwd) {
        callback(new Error(this.$t('liangCiShuRuMiMaBuYiZhi')));
      } else {
        callback();
      }
    };
    const JavaMerInfo = JSON.parse(Cookies.get('JavaMerInfo'));
    console.log('JavaMerInfo', JavaMerInfo);
    return {
      password: '',
      JavaMerInfo: JSON.parse(Cookies.get('JavaMerInfo')),
      pram: {
        account: JavaMerInfo.account,
        pwd: null,
        repwd: null,
        realName: JavaMerInfo.realName,
        id: JavaMerInfo.id,
        code: JavaMerInfo.code,
      },
      roleList: [],
      rules: {
        account: [{ required: true, message: this.$t('qingTianGuanLiYuanZhangHao'), trigger: ['blur', 'change'] }],
        pwd: [{ required: true, message: this.$t('qingTianGuanLiYuanMiMa'), trigger: ['blur', 'change'] }],
        repwd: [
          { required: true, message: this.$t('queRenMiMaMiMa'), validator: validatePass, trigger: ['blur', 'change'] },
        ],
        realName: [{ required: true, message: this.$t('guanLiYuanXingMing'), trigger: ['blur', 'change'] }],
      },
    };
  },
  methods: {
    close(formName) {
      this.$refs[formName].resetFields();
    },
    handlerSubmit: Debounce(function (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          systemAdminApi.adminAccountUpdate(this.pram).then((data) => {
            this.$message.success(this.$t('tiJiaoChengGong'));
            this.$router.go(-1);
          });
        } else {
          return false;
        }
      });
    }),
    handlerPwdInput(val) {
      if (!val) {
        this.rules.pwd = [];
        this.rules.repwd = [];
        return;
      }
      this.rules.pwd = [
        { required: true, message: this.$t('qingTianGuanLiYuanMiMa'), trigger: ['blur', 'change'] },
        { min: 6, max: 20, message: this.$t('changDu_620GeZiFu'), trigger: ['blur', 'change'] },
      ];
      this.rules.repwd = [
        {
          required: true,
          message: this.$t('liangCiShuRuMiMaBuYiZhi0'),
          validator: (rule, value, callback) => {
            if (value === '') {
              callback(new Error(this.$t('liangCiShuRuMiMaBuYiZhi')));
            } else if (value !== this.pram.pwd) {
              callback(new Error(this.$t('liangCiShuRuMiMaBuYiZhi')));
            } else {
              callback();
            }
          },
          trigger: ['blur', 'change'],
        },
      ];
    },
  },
};
</script>

<style scoped></style>
